package com.yiya.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiya.bean.Product;
import com.yiya.bean.SysUser;
import com.yiya.mapper.ProductMapper;
import com.yiya.model.PriceRelModel;
import com.yiya.model.ProductCplex;
import com.yiya.model.ProductModel;

@Service("productService")
public class ProductService {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(MerchantService.class);

	@Autowired
	private ProductMapper productMapper;

	public List<ProductCplex> queryByList(ProductModel bean) {
		Integer rowCount = queryByCount(bean);
		bean.getPager().setRowCount(rowCount);
		List<ProductCplex> products = productMapper.selectByModel(bean);
		return products;
	}

	public int queryByCount(ProductModel bean) {
		return productMapper.countByModel(bean);
	}

	public int add(Product product) {
		return productMapper.insert(product);
	}

	public int Update(Product product) {
		return productMapper.updateByPrimaryKeySelective(product);
	}

	public Product queryById(Integer id) {
		return productMapper.selectByPrimaryKey(id);
	}

	public void delete(Integer[] ids, SysUser user) {
		Date nowTime = new Date();
		for (Integer id : ids) {
			Product product = productMapper.selectByPrimaryKey(id);
			product.setUpdateBy(user.getId());
			product.setUpdateTime(nowTime);
			product.setDelFlag(1);
			productMapper.updateByPrimaryKeySelective(product);
		}
	}

	public List<Product> queryAllList(ProductModel model) {
		return productMapper.queryAllList(model);
	}

	public List<Product> queryAllListByMerchant(PriceRelModel model) {
		return productMapper.queryAllListByMerchant(model);
	}
	
	public Product queryByCode(ProductModel product){
		return productMapper.queryByCode(product);
	}
	
	/**
	 * 获取id不相等，code相等的产品
	 * @param record
	 * @return
	 */
	public List<Product> queryOther(ProductModel record){
		return productMapper.queryOther(record);
	}

}
